/* RGB Fading
 
 This example shows how to fade an LED using the analogWrite() function.
 
 The circuit:
 * Green LED attached from digital pin 9 to ground.
 * Blue LED  attached from digital pin 10 to ground.
 * Red LED   attached from digital pin 11 to ground.
 
 */

#include <EEPROM.h>
const int maxdelay = 99;
const int maxmode = 4;
const int LedVcc = 8;    // LED connected to digital pin 11
const int redpin = 11;    // LED connected to digital pin 11
const int bluepin = 10;    // LED connected to digital pin 10
const int greenpin = 9;     // LED connected to digital pin 9
int oldr = 254;
int oldg = 254;
int oldb = 254;
int mode = 1;
int delayval = 20; // 1;
char* modes[]={"","Fade smoothly with variable delay","Pulse in and out","Fade in steps--broken","Blink"};

void setup()  { 
  // nothing happens in setup
 analogWrite(LedVcc,oldr); 
  analogWrite(redpin,oldr);
  analogWrite(bluepin,oldb);
  analogWrite(greenpin,oldg);
  randomSeed(analogRead(0));
  Serial.begin(9600);	// opens serial port, sets data rate to 9600 bps
  //delayval = constrain(EEPROM.read(0),1,maxdelay);
  //mode = constrain(EEPROM.read(1),1,maxmode);
  //Serial.println("Reading settings from EEPROM...");
  //Serial.print("Mode is ");
  //Serial.println(mode);
  //Serial.print("Delay is ");
  //Serial.println(delayval);
} 

void loop()  { 
  // analogWrite(LedVcc, 155);
   digitalWrite(LedVcc, HIGH);
    
    
    int intensity = 255;
   
    
 int newr = random(10,intensity);
  int newg = random(10,intensity);
  int newb = random(10,intensity);
  
  int scale = 71;
  for (int i = 0; i <= scale; i +=1) {
    analogWrite(redpin, 255-(i*(newr/scale)));
    analogWrite(greenpin, 255-(i*(newg/scale)));
    analogWrite(bluepin, 255-(i*(newb/scale)));
    delay(delayval);
  }
  
  for (int i = scale; i > 5; i -=1) {
    analogWrite(redpin, 255-(i*(newr/scale)));
    analogWrite(greenpin, 255-(i*(newg/scale)));
    analogWrite(bluepin, 255-(i*(newb/scale)));
//    Serial.println(newr);
//    Serial.println(newg);
//    Serial.println(newb);
    
    delay(delayval);
     delay(delayval);
      delay(delayval);
     delay(delayval);
  }
}

void random_fade1() {
 
}
void random_fade2() {
  int newr = random(1,255);
  int newg = random(1,255);
  int newb = random(1,255);


  int scale = 255;
  float rscale=(oldr-newr)/scale;
  float gscale=(oldg-newg)/scale;
  float bscale=(oldb-newb)/scale;
  for (int i = 0; i <= scale; i +=1) {
    analogWrite(redpin, oldr-int(float(i)*rscale));
    analogWrite(greenpin, oldg-int(float(i)*gscale));
    analogWrite(bluepin, oldb-int(float(i)*bscale));
    delay(delayval);
  }
  

  oldb = newb;
  oldr = newr;
  oldg = newg;
}

void random_fade3(int delayval) {
  int newr = random(1,255);
  int newg = random(1,255);
  int newb = random(1,255);

  fade_one(delayval, redpin, oldr, newr);  
  fade_one(delayval, greenpin, oldg, newg);  
  fade_one(delayval, bluepin, oldb, newb);
  
  oldb = newb;
  oldr = newr;
  oldg = newg;
}

void fade_one(int delayval, int pin, int oldval, int newval) {
  if (oldval > newval) {
    for (int x = oldval; x >= newval; x -=1) {
      analogWrite(pin, x);
      delay (delayval);
    }
  } else {
    for (int x =oldval; x <= newval; x += 1) {
      analogWrite(pin,x);
      delay (delayval);
    }
  }
}

void random_blink() {
  int newr = random(200,255);
  int newg = random(200,255);
  int newb = random(200,255);
  analogWrite(redpin, newr);
  analogWrite(bluepin, newb);
  analogWrite(greenpin, newg);
  
  delay(delayval*10);

}


